package cn.wzb._02_workqueue;

import cn.wzb.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

//消息接收方
public class Rev2 {

    public static void main(String[] args) throws Exception {

        //1.创建链接
        Connection connection = ConnectionUtil.getConnection();
        //2.创建通道
        Channel channel = connection.createChannel();
        //3.声明队列
        channel.queueDeclare(Send.QUEUE,true,false,false,null);

        //同一时间只能处理一个消息
        channel.basicQos(1);

        //拿到消息之后的回调，用来消费消息的。
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                System.out.println("消费者的标签："+consumerTag);
                System.out.println("队列RoutingKey："+envelope.getRoutingKey());
                System.out.println("交换机Exchange："+envelope.getExchange());
                System.out.println("消息的ID："+envelope.getDeliveryTag());
                System.out.println("消息的内容："+new String(body));

            }
        };
        //4.消费消息 ： 队列名 ， 签收方式 ， 消费回调
        //拿到消息之后,自动签收,MQ会把消息删除
        //channel.basicConsume(Producer.QUEUE , true,consumer);
        channel.basicConsume(Send.QUEUE , true,consumer);
    }
}
